package module.common.share;

import java.util.Set;

import library.common.util.SharedUtils;
import module.common.net.data.BeanForRefreshToken;

/**
 * @author Created by qiang.hou on 2018/12/4.
 * @version 1.0
 */
public class SharedManager {
    /**
     * String
     */
    public final static String KEY_LOGIN_NAME = "key_login_name";
    /**
     * boolean
     */
    public final static String KEY_IS_FIRST_START_APP = "key_is_first_start_app";
    /**
     * String
     */
    public final static String KEY_VERSION_NAME = "key_version_name";
    /**
     * boolean
     */
    public final static String KEY_IS_LOGIN = "key_is_login";

    /*************** for user info ***************/
    /**
     * long
     */
    public final static String KEY_TOKEN_EXPIRES_TIME = "key_token_expires_time";
    /**
     * String
     */
    public final static String KEY_TOKEN_TYPE = "key_token_type";
    /**
     * String
     */
    public final static String KEY_TOKEN_VALUE = "key_token_value";
    /**
     * String
     */
    public final static String KEY_REFRESH_TOKEN_VALUE = "key_refresh_token_value";
    /**
     * int
     */
    public final static String KEY_USER_STATUS = "key_user_status";
    /**
     * String
     */
    public static final String KEY_USER_SCOPE = "key_user_scope";
    /**
     * String
     */
    public static final String KEY_USER_SUB = "key_user_sub";
    /**
     * String
     */
    public static final String KEY_USER_ENTERPRISE_STATUS = "key_user_userEnterpriseStatus";
    /**
     * String
     */
    public static final String KEY_USER_AUTO = "key_user_auto";
    /**
     * String
     */
    public static final String KEY_USER_FULL_NAME = "key_user_userFullName";
    /**
     * String
     */
    public static final String KEY_USER_ROLE_ID = "key_user_roleId";//11:ADMIN  12:OPERATION
    /**
     * String
     */
    public static final String KEY_USER_ENTERPRISE_ID = "key_user_enterpriseId";
    /**
     * String
     */
    public static final String KEY_USER_ENTERPRISE_NAME = "key_user_enterpriseName";
    /**
     * String
     */
    public static final String KEY_USER_RIGHT = "key_user_right";
    /**
     * String
     */
    public static final String KEY_USER_RIGHT_MENU = "key_user_rightMenu";
    /**
     * String
     */
    public static final String KEY_USER_NAME = "key_user_username";
    /**
     * String
     */
    public static final String KEY_USER_ID = "key_user_userId";
    /**
     * String
     */
    public static final String KEY_USER_JTI = "key_user_jti";

    private SharedManager() {
    }

    private static class SharedManagerHolder {
        private static SharedManager INSTANCE = new SharedManager();
    }

    public static SharedManager getInstance() {
        return SharedManagerHolder.INSTANCE;
    }

    public String getValue(String key, String defaultValue) {
        return SharedUtils.getString(key, defaultValue);
    }

    public int getValue(String key, int defaultValue) {
        return SharedUtils.getInt(key, defaultValue);
    }

    public long getValue(String key, long defaultValue) {
        return SharedUtils.getLong(key, defaultValue);
    }

    public boolean getValue(String key, boolean defaultValue) {
        return SharedUtils.getBoolean(key, defaultValue);
    }

    public Set<String> getValue(String key, Set<String> defaultValue) {
        return SharedUtils.getStringSet(key, defaultValue);
    }

    public void setValue(String key, String value) {
        SharedUtils.setString(key, value);
    }

    public void setValue(String key, int value) {
        SharedUtils.setInt(key, value);
    }

    public void setValue(String key, long value) {
        SharedUtils.setLong(key, value);
    }

    public void setValue(String key, boolean value) {
        SharedUtils.setBoolean(key, value);
    }

    public void setValue(String key, Set<String> value) {
        SharedUtils.setStringSet(key, value);
    }

    /**
     * commit token info
     *
     * @param tokenInfo BeanForRefreshToken
     */
    public void refreshToken(BeanForRefreshToken tokenInfo) {
        long expires_time = System.currentTimeMillis() + tokenInfo.expiresIn * 1000 - 60000 * 60;//提前一小时
        setValue(KEY_TOKEN_EXPIRES_TIME, expires_time);
        setValue(KEY_TOKEN_TYPE, tokenInfo.tokenType);
        setValue(KEY_TOKEN_VALUE, tokenInfo.value);
        setValue(KEY_REFRESH_TOKEN_VALUE, tokenInfo.refreshToken.value);
    }

    /**
     * clear user info
     */
    public void clearUserData() {
        setValue(KEY_TOKEN_VALUE, "");
        setValue(KEY_TOKEN_TYPE, "");
        setValue(KEY_REFRESH_TOKEN_VALUE, "");
        setValue(KEY_TOKEN_EXPIRES_TIME, 0);
//        setValue(EXPIRES_TIME, 0L);
//        setValue(USER_STATUS, 0);
//        setValue(SCOPE, "");
//        setValue(SUB, "");
//        setValue(USER_ENTERPRISE_STATUS, "");
//        setValue(AUTO, "");
//        setValue(USER_FULL_NAME, "");
//        setValue(ENTERPRISE_ID, "");
//        setValue(ENTERPRISE_NAME, "");
//        setValue(RIGHT, "");
//        setValue(USER_NAME, "");
//        setValue(JTI, "");
//        setValue(IS_LOGIN, false);
    }
}

